package com.astruyk.battlegroundAndroid;

import android.app.Activity;
import android.app.AlertDialog;
import android.app.Dialog;
import android.database.Cursor;
import android.net.Uri;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.widget.AdapterView;
import android.widget.BaseAdapter;
import android.widget.ListView;
import android.widget.TextView;
import android.widget.AdapterView.OnItemClickListener;
import com.astruyk.battlegroundAndroid.data.CapturePoint;
import com.astruyk.battlegroundAndroid.data.TownUpdater;

public class TownListActivity extends Activity{

	ListView cpListView;
	//SimpleCursorAdapter cpAdapter;
	BaseAdapter cpAdapter;
	long selectedCp;

	static final private int MENU_REFRESH = 0;
	static final private int CP_DETAILS_DIALOG = 1;
	
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		
		setContentView(R.layout.town_list);
		
		cpListView = (ListView)findViewById(R.id.townListView);
		cpListView.setOnItemClickListener(new OnItemClickListener(){
			@Override
			public void onItemClick(AdapterView<?> arg0, View view, int position, long id) {
				selectedCp = id;
				showDialog(CP_DETAILS_DIALOG);
			}
		});
		
		cpAdapter = new TownListRowAdapter(this);
		cpListView.setAdapter(cpAdapter);
	}
	
	private void refreshTownList(){
		TownUpdater.instance.refreshTownList(
				this,
				new TownUpdater.AsyncResult(){
					@Override
					public void operationComplete() {
						cpAdapter.notifyDataSetChanged();
					}
				});
	}

	@Override
	public boolean onCreateOptionsMenu(Menu menu) {
		super.onCreateOptionsMenu(menu);
		MenuItem newItem = menu.add(0, MENU_REFRESH, Menu.NONE, R.string.refresh_option);
		newItem.setIcon(android.R.drawable.ic_menu_revert);
		return true;
	}

	@Override
	public boolean onMenuItemSelected(int featureId, MenuItem item) {
		super.onMenuItemSelected(featureId, item);
		
		switch(item.getItemId()){
		case MENU_REFRESH:
			refreshTownList();
			return true;
		}
		
		return false;
	}

	@Override
	protected Dialog onCreateDialog(int id) {
		super.onCreateDialog(id);
		switch(id){
		case CP_DETAILS_DIALOG:
			LayoutInflater layoutInflater = LayoutInflater.from(this);
			View townDetailsView = layoutInflater.inflate(R.layout.town_details, null);
			
			AlertDialog.Builder detailsDialog = new AlertDialog.Builder(this);
			detailsDialog.setTitle("Town Details");
			detailsDialog.setView(townDetailsView);
			return detailsDialog.create();
		}
		return null;
	}

	@Override
	protected void onPrepareDialog(int id, Dialog dialog) {
		super.onPrepareDialog(id, dialog);
		
		switch(id){
		case CP_DETAILS_DIALOG:
			Uri cpUri = Uri.withAppendedPath(CapturePoint.CONTENT_URI, String.valueOf(selectedCp));
			String[] columns = new String[] { 
					CapturePoint.NAME,
					CapturePoint.TYPE,
					CapturePoint.LINKS,
					CapturePoint.X,
					CapturePoint.Y,
					CapturePoint.ABS_X,
					CapturePoint.ABS_Y,
					CapturePoint.ORIG_COUNTRY,
					CapturePoint.ORIG_SIDE
			};
			Cursor cursor = getContentResolver().query(cpUri, columns, null, null, null);
			
			final int nameColumn = cursor.getColumnIndex(CapturePoint.NAME);
			int currentColumn = nameColumn;
			
			AlertDialog detailsDialog = (AlertDialog)dialog;
			detailsDialog.setTitle(currentColumn++);
			
			StringBuilder builder = new StringBuilder();
			builder.append("type:  ").append(currentColumn++).append('\n');
			builder.append("links: ").append(currentColumn++).append('\n');
			builder.append('\n');
			builder.append("X:    ").append(currentColumn++).append('\n');
			builder.append("Y:    ").append(currentColumn++).append('\n');
			builder.append("absX: ").append(currentColumn++).append('\n');
			builder.append("absY: ").append(currentColumn++).append('\n');
			builder.append('\n');
			builder.append("orig-country: ").append(currentColumn++).append('\n');
			builder.append("orig-side:    ").append(currentColumn++).append('\n');
			
			TextView tv = (TextView) detailsDialog.findViewById(R.id.townDetailsTextView);
			tv.setText(builder);
			break;
		}
	}
	
	
	
}
